<?php
    class Pager
    {
        function findStart($limit)
        {
            if ((!isset($_GET['page'])) || ($_GET['page'] == "1"))
            {
                $start = 0;
                $_GET['page'] = 1;
            }
            else
            {
                $start = ($_GET['page']-1) * $limit;
            }
            return $start;
        }

        function findPages($count, $limit)
        {
            $pages = ceil($count / $limit);
            return $pages;
        }

        function pageList($curpage, $pages, $lastStr)
        {
            $page_list = '';

            for ($i=1; $i<=$pages; $i++)
            {
                if ($i == $curpage)
                {
                    $page_list .= '<li><span>'.$i.'</span></li>';
                }
                else
                {
                    $page_list .= '<li><a href="'. WEB_BASE_URL .'/index.php?page=' . $i . '&' . $lastStr . '" title="page '.$i.'" >'.$i.'</a></li>';
                }
                $page_list .= " ";
            }

            return $page_list;
        }

        function findNext ($curpage, $pages, $lastStr)
        {
            if (($curpage + 1) > $pages)
            {
                $str = '<span class="PagingNextPageLnk">Next</span>';
            }
            else
            {
                $next = $curpage + 1;
               $str = '<a class="PagingNextPageLnk" title="Đến trang tiếp theo" href="'. WEB_BASE_URL .'/index.php?page=' . $next . '&' . $lastStr . '" >Next</a>';
            }

            return $str;
        }

        function findLast ($curpage, $pages, $lastStr)
        {
            if ($curpage  >= $pages)
            {
                $str = '<span class="PagingLastPageLnk">Last</span>';
            }
            else
            {
                $str = '<a class="PagingLastPageLnk" title="Đến trang cuối cùng" href="'. WEB_BASE_URL .'/index.php?page=' . $pages . '&' . $lastStr . '" >Last</a>';
            }

            return $str;
        }

        function findPrev ($curpage, $pages, $lastStr)
        {
            if (($curpage - 1) <= 0)
            {
                $str = '<span class="PagingPrevPageLnk">Prev</span>';
            }
            else
            {
                $prev = $curpage - 1;
                $str = '<a class="PagingPrevPageLnk" title="Trở về trang trước" href="'. WEB_BASE_URL .'/index.php?page=' . $prev . '&' . $lastStr . '" >Prev</a>';
            }

            return $str;
        }

        function findFirst ($curpage, $pages, $lastStr)
        {
            if ($curpage  <= 1)
            {
                $str = '<span class="PagingFirstPageLnk">First</span>';
            }
            else
            {
                $str = '<a class="PagingFirstPageLnk" title="Trở về trang đầu tiên" href="'. WEB_BASE_URL .'/index.php?page=1&' . $lastStr . '" >First</a>';
            }

            return $str;
        }

        function nextPrev($curpage, $pages, $dk)
        {
            $next_prev = "";

            if (($curpage-1) <= 0)
            {
                $next_prev .= "<font color='#FF0099'>Prev</font>";
            }
            else
            {
                $next_prev .= '<a href="index.php?page='.($curpage-1).'&{$dk}" >Prev</a>';
            }

            $next_prev .= " | ";

            if (($curpage+1) > $pages)
            {
                $next_prev .= "<font color='#FF0099'>Next/font>";
            }
            else
            {
                $next_prev .= '<a href="index.php?page='.($curpage+1).'&{$dk}" >Next</a>';
            }

            return $next_prev;

        }
    }
?>